package etc.com.market;

import Controls.JsonDataHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.TextView;
import etc.com.sql.DBAdapter;

public class GalleryAct extends Activity {
	/** Called when the activity is first created. */
	Bitmap[] ImageList;
	int ItemsInGallery = 0;
	int CurrentIndex = 0;
	TextView txt;
	Gallery gallery;
	String storeName = "";
	String prodId = "";
	int position = 0;
	private static final int REQUEST_CODE = 10;

	public void galleryToStoreList(View view) {
		Intent i = new Intent(this, StoreListAct.class);
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void clickImageGallery(View view ) {
		// TODO Auto-generated method stub
		gallery = (Gallery) findViewById(R.id.gallery);
		// ItemsInGallery = gallery.getCount() / 2;
		if(view.getId() == R.id.search || view.getId() == R.id.btnNext){
			CurrentIndex++;
			// if reached the end of the gallery, then start from the first item
			if (CurrentIndex >= ItemsInGallery)
				CurrentIndex = 0;
			gallery.setSelection(CurrentIndex, true);
		}else if(view.getId() == R.id.btnPre || view.getId() == R.id.home){
			CurrentIndex = CurrentIndex - 1;
			// If reached the first item, then return to the last item in the
			// gallery
			if (CurrentIndex < 0)
				CurrentIndex = ItemsInGallery - 1;
			gallery.setSelection(CurrentIndex, true);
		}
		TextView v1 = (TextView) findViewById(R.id.storename);
		v1.setText(String.valueOf(CurrentIndex + 1) + "/" + ImageList.length);
	}
	
	
	
	
	// function use for footer button ==============================
		public void Previous(View view) {
			//onClickPre(view, ("Store Info;"));
			finish();
			overridePendingTransition(R.anim.fadein,
                    R.anim.fadeout);
		}
		
		public void Home(View view) {
			Intent i = new Intent(this, StoreTopPage.class);
			i.putExtra("message", storeName);
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}
		
		
	   //===================================================================
		
		
		 
		 public void onClickPre(View view, String message) {
				Intent i = new Intent(this, moreImage.class);
				i.putExtra("message", message);
				// Set the request code to any code you like, you can identify the
				// callback via this code
				startActivity(i);
				overridePendingTransition(R.anim.fadein,
		                R.anim.fadeout);

			}
		 
		 public void onClickHome(View view, String message) {
				Intent i = new Intent(this, StoreListAct.class);
				i.putExtra("message", message);
				// Set the request code to any code you like, you can identify the
				// callback via this code
				startActivity(i);
				overridePendingTransition(R.anim.fadein,
		                R.anim.fadeout);
			}
		 	
	
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.gallery);
		Gallery gallery = (Gallery) findViewById(R.id.gallery);
		
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}

//		ImageList = extras.getIntArray("message");
		prodId = extras.getString("productId");
		DBAdapter dbAdapter = new DBAdapter(getApplicationContext());
		Cursor cursor = dbAdapter.getProductDetailsById(prodId);
		cursor.moveToNext();
		
		String imageListString = cursor.getString(8);
		imageListString = imageListString.substring(1, imageListString.length() - 1);
		String[] imagePaths = imageListString.split(",");
		ImageList = new Bitmap[imagePaths.length];
		for(int i = 0; i < imagePaths.length; i++){
			String imagePath = imagePaths[i].trim().substring(1, imagePaths[i].trim().length() - 1);
			String imageValue = JsonDataHelper.getImageContent(imagePath);
			byte[] decodeBytes = Base64.decode(imageValue.getBytes(), Base64.DEFAULT);
			ImageList[i] = BitmapFactory.decodeByteArray(decodeBytes, 0, decodeBytes.length);
		}
		
		cursor.close();
		position = extras.getInt("selected");
		ItemsInGallery = extras.getInt("arraySize");
		CurrentIndex = position;
		storeName = extras.getString("storeName");
		gallery.setAdapter(new ImageAdapter(this, ImageList));
		gallery.setSelection(position);
		if(position < 0){
			position = 0;
		}
		
		String currentIndex = String.valueOf(CurrentIndex + 1);
		TextView v1 = (TextView) findViewById(R.id.storename);
		v1.setText(currentIndex + "/" + ImageList.length);

		gallery.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				onClick(parent,ImageList[position]);
				// TextView v1 = (TextView) findViewById(R.id.storename);
				// v1.setText(position + ImageList.length);

			}
		});
		
		
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				TextView v1 = (TextView) findViewById(R.id.storename);
				v1.setText(String.valueOf(arg2 + 1) + "/" + ImageList.length);
				CurrentIndex = arg2;
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
			
		});
	}

	public void onClick(View v,Bitmap  resource) {
		// TODO Auto-generated method stub
		Intent i = new Intent(this, ImageZoomActivity.class);
		i.putExtra("productId", prodId);
		i.putExtra("imageIndex", position);
//		i.putExtra("image", resource);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
		return;
		
//		
//		ItemsInGallery = gallery.getCount() / 2;
//		switch (v.getId()) {
//		case R.id.search:// next
//			// Increase the index
//			CurrentIndex++;
//			// if reached the end of the gallery, then start from the first item
//			if (CurrentIndex > ItemsInGallery - 1)
//				CurrentIndex = 0;
//			gallery.setSelection(CurrentIndex, true);
//			break;
//		case R.id.btnPre:// previuos
//			// Decrease the index
//			CurrentIndex = CurrentIndex - 1;
//			// If reached the first item, then return to the last item in the
//			// gallery
//			if (CurrentIndex < 0)
//				CurrentIndex = ItemsInGallery - 1;
//			gallery.setSelection(CurrentIndex, true);
//
//			break;
//		}

	}
	
}